<template>
  <div class="flight-item">
    <div>
      <!-- 显示的机票信息 -->
      <el-row type="flex" align="middle" class="flight-info" @click.native="isShow=!isShow">
        <el-col :span="6">
          <span>{{ data.airline_name }}</span>
          {{ data.flight_no }}
        </el-col>
        <el-col :span="12">
          <el-row type="flex" justify="space-between" class="flight-info-center">
            <el-col :span="8" class="flight-airport">
              <strong>{{ data.dep_time }}</strong>
              <span>{{ data.org_airport_name }}{{ data.org_airport_quay }}</span>
            </el-col>
            <el-col :span="8" class="flight-time">
              <span>{{ duration }}</span>
            </el-col>
            <el-col :span="8" class="flight-airport">
              <strong>{{ data.arr_time }}</strong>
              <span>{{ data.dst_airport_name }}{{ data.dst_airport_quay }}</span>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="6" class="flight-info-right">
          ￥
          <span class="sell-price">{{ data.base_price }}</span>起
        </el-col>
      </el-row>
    </div>

    <el-collapse-transition>
      <div class="flight-recommend" v-if="isShow">
        <!-- 隐藏的座位信息列表 -->
        <el-row type="flex" justify="space-between" align="middle">
          <el-col :span="4">低价推荐</el-col>
          <el-col :span="20">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="flight-sell"
              v-for="(item,index) in data.seat_infos"
              :key="index"
            >
              <el-col :span="16" class="flight-sell-left">
                <span>{{ item.name }}</span>
                | {{ item.supplierName }}
              </el-col>
              <el-col :span="5" class="price">￥{{ item.org_settle_price }}</el-col>
              <el-col :span="3" class="choose-button">
                <el-button type="warning" size="mini" @click="order(item)">
                  选定
                </el-button>
                <p v-if="item.nums!='A'">剩余：{{ item.nums }}</p>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </div>
    </el-collapse-transition>
  </div>
  
</template>

<script>
export default {
  data() {
    return {
      isShow: false
    };
  },
  props: {
    // 数据
    data: {
      type: Object,
      // 默认是空数组
      default: () => {
        return {};
      }
    }
  },
  computed: {
    duration() {
      // 到达时间(假设22:50)
      let arrivalTime =
        Number(this.data.arr_time.split(":")[0]) * 60 +
        Number(this.data.arr_time.split(":")[1]);
      // 出发时间(假设20:30)
      let departureTime =
        Number(this.data.dep_time.split(":")[0]) * 60 +
        Number(this.data.dep_time.split(":")[1]);
      // console.log(arrivalTime, departureTime);

      // 判断如果时长差值为负数时，+24小时再相减
      if (arrivalTime - departureTime < 0) {
        arrivalTime += 24 * 60;
      }

      // 返回一个时长差值
      return (
        ((arrivalTime - departureTime) / 60).toFixed(0) +
        "小时" +
        ((arrivalTime - departureTime) % 60) +
        "分"
      );
    }
  },

  methods: {
    order(item){
      // 当点击[选定]时，跳转路由需要两个参数
      // 1.航班ID
      console.log('航班ID',this.data.id);
      // 2.座位ID(传参时带上item)
      console.log('座位ID',item.seat_xid);

      this.$router.push(`/air/order/?id=${this.data.id}&seat_xid=${item.seat_xid}`)
    }
  }
};
</script>

<style scoped lang="less">
.flight-item {
  border: 1px #ddd solid;
  margin-bottom: 10px;
  background-color: #fff;

  .flight-info {
    padding: 15px;
    cursor: pointer;

    > div {
      &:first-child,
      &:last-child {
        text-align: center;
      }
    }
  }

  .flight-info-center {
    padding: 0 30px;
    text-align: center;

    .flight-airport {
      strong {
        display: block;
        font-size: 24px;
        font-weight: normal;
      }
      span {
        font-size: 12px;
        color: #999;
      }
    }

    .flight-time {
      span {
        display: inline-block;
        padding: 10px 0;
        border-bottom: 1px #eee solid;
        color: #999;
      }
    }
  }

  .flight-info-right {
    .sell-price {
      font-size: 24px;
      color: orange;
      margin: 0 2px;
    }
  }
}

.flight-recommend {
  background: #f6f6f6;
  border-top: 1px #eee solid;
  padding: 0 20px;

  .flight-sell {
    border-bottom: 1px #eee solid;
    padding: 10px 0;

    &:last-child {
      border-bottom: none;
    }

    .flight-sell-left {
      font-size: 12px;
      span {
        color: green;
      }
    }

    .price {
      font-size: 20px;
      color: orange;
    }

    .choose-button {
      text-align: center;
      color: #666;
      button {
        display: block;
        width: 100%;
        margin-bottom: 5px;
      }
    }
  }
}
</style>